﻿package core.image
{
    import __AS3__.vec.*;
    import core.enum.*;
    import flash.display.*;
    import flash.filters.*;
    import flash.geom.*;

    public class Misc extends Object
    {
        public static const OVERSAMPLING:int = 4;
        static var a1:int;
        static var r1:int;
        static var g1:int;
        static var b1:int;
        static var a2:int;
        static var r2:int;
        static var g2:int;
        static var b2:int;
        static var fx:int;
        static var fy:int;
        static var cx:int;
        static var cy:int;
        static var lx:Number;
        static var ly:Number;
        static var p1:uint;
        static var p2:uint;

        public function Misc()
        {
            return;
        }// end function

        public static function Feather(g1:BitmapData, g1:int) : void
        {
            var _loc_3:* = new BlurFilter(g1, g1, 3);
            g1.applyFilter(g1, g1.rect, g1.rect.topLeft, _loc_3);
            return;
        }// end function

        public static function DeAntialias(g1:BitmapData) : void
        {
            var _loc_2:* = new ColorMatrix();
            _loc_2.AlphaThreshold(64);
            g1.applyFilter(g1, g1.rect, g1.rect.topLeft, _loc_2.filter);
            return;
        }// end function

        public static function ComputePaletteArray(fy:Array, fy:int) : Array
        {
            var _loc_3:int = 0;
            while (_loc_3 < 256)
            {
                
                fy[_loc_3] = fy[_loc_3] << fy;
                _loc_3++;
            }
            return fy;
        }// end function

        public static function TrimBitmap(g1:Bitmap) : void
        {
            var _loc_2:Rectangle = null;
            var _loc_3:BitmapData = null;
            if (g1.bitmapData != null && g1.width > 1 && g1.height > 1)
            {
                _loc_2 = g1.bitmapData.getColorBoundsRect(4294967295, 16777215, false);
                if (_loc_2.width > 0 && _loc_2.height > 0)
                {
                    if (!_loc_2.equals(new Rectangle(g1.x, g1.y, g1.width, g1.height)))
                    {
                        g1.x = g1.x + _loc_2.x;
                        g1.y = g1.y + _loc_2.y;
                        _loc_3 = g1.bitmapData.clone();
                        g1.bitmapData = new BitmapData(_loc_2.width, _loc_2.height, true, 16777215);
                        g1.bitmapData.copyPixels(_loc_3, _loc_2, _loc_3.rect.topLeft);
                    }
                }
                else
                {
                    g1.bitmapData.dispose();
                    g1.bitmapData = null;
                }
            }
            return;
        }// end function

        public static function InflateBitmap(g1:Bitmap, g1:Rectangle) : void
        {
            var _loc_3:BitmapData = null;
            if (g1.bitmapData != null)
            {
                _loc_3 = g1.bitmapData.clone();
                g1.bitmapData = new BitmapData(g1.width, g1.height, true, 16777215);
                g1.bitmapData.copyPixels(_loc_3, _loc_3.rect, new Point(g1.x < 0 ? (-g1.x) : (0), g1.y < 0 ? (-g1.y) : (0)));
            }
            return;
        }// end function

        public static function ApplySelection(g1:BitmapData, g1:BitmapData, g1:Selection, g1:Point) : void
        {
            var _loc_5:BitmapData = null;
            var _loc_6:BitmapData = null;
            if (g1 != null)
            {
                _loc_5 = g1.clone();
                _loc_6 = g1.AlphaMask(new Rectangle(g1.x, g1.y, g1.width, g1.height));
                g1.copyPixels(g1, g1.rect, g1.rect.topLeft);
                g1.copyPixels(_loc_5, _loc_5.rect, _loc_5.rect.topLeft, _loc_6, _loc_6.rect.topLeft, true);
            }
            return;
        }// end function

        public static function ApplySelectionMask(g1:BitmapData, g1:Selection, g1:Point) : void
        {
            var _loc_4:BitmapData = null;
            var _loc_5:BitmapData = null;
            if (g1 != null)
            {
                _loc_4 = g1.clone();
                _loc_5 = g1.AlphaMask(new Rectangle(g1.x, g1.y, g1.width, g1.height));
                g1.fillRect(g1.rect, 16777215);
                g1.copyPixels(_loc_4, _loc_4.rect, _loc_4.rect.topLeft, _loc_5, _loc_5.rect.topLeft, true);
            }
            return;
        }// end function

        public static function Clear(g1:BitmapData, g1:Selection, g1:Point, g1:Boolean) : void
        {
            var _loc_5:BitmapData = null;
            var _loc_6:BitmapData = null;
            if (g1 != null)
            {
                if (!g1)
                {
                    _loc_5 = g1.clone();
                    g1.fillRect(g1.rect, 4294967295);
                    ApplySelection(g1, _loc_5, g1, g1);
                }
                else
                {
                    _loc_5 = g1.clone();
                    _loc_6 = g1.InvertedAlphaMask(new Rectangle(g1.x, g1.y, g1.width, g1.height));
                    g1.fillRect(g1.rect, 16777215);
                    g1.copyPixels(_loc_5, _loc_5.rect, _loc_5.rect.topLeft, _loc_6, _loc_6.rect.topLeft, true);
                }
            }
            return;
        }// end function

        public static function GetAffectedRect(cx:BitmapData, cx:Selection, cx:Point) : Rectangle
        {
            if (cx == null)
            {
                return cx.rect;
            }
            var _loc_4:* = new Rectangle(cx.x, cx.y, cx.width, cx.height);
            var _loc_5:* = cx.rect.intersection(_loc_4);
            _loc_4 = cx.rect.intersection(_loc_4);
            _loc_5.x = _loc_4.x - cx.x;
            _loc_4.y = _loc_4.y - cx.y;
            return _loc_4;
        }// end function

        public static function CalculateRectangle(cx:Point, cx:Point, cx:int = 0, cx:int = 0) : Rectangle
        {
            var _loc_5:* = new Rectangle(cx.x, cx.y, 1, 1).union(new Rectangle(cx.x, cx.y, 1, 1));
            if (cx != 0 && cx != 0)
            {
                if (cx / cx * (_loc_5.width / _loc_5.height) < cx / cx * (_loc_5.height / _loc_5.width))
                {
                    _loc_5.height = Math.round(_loc_5.width * (cx / cx));
                    if (cx.y > cx.y)
                    {
                        _loc_5.y = cx.y - _loc_5.height + 1;
                    }
                }
                else
                {
                    _loc_5.width = Math.round(_loc_5.height * (cx / cx));
                    if (cx.x > cx.x)
                    {
                        _loc_5.x = cx.x - _loc_5.width + 1;
                    }
                }
            }
            return _loc_5;
        }// end function

        public static function CombinePixels(Array:uint, Array:uint, Array:Number) : uint
        {
            if (Array == 1)
            {
                return Array;
            }
            if (Array == Array)
            {
                return Array;
            }
            a1 = Array >>> 24 & 255;
            r1 = Array >>> 16 & 255;
            g1 = Array >>> 8 & 255;
            b1 = Array & 255;
            a2 = Array >>> 24 & 255;
            r2 = Array >>> 16 & 255;
            g2 = Array >>> 8 & 255;
            b2 = Array & 255;
            a1 = a1 + a2;
            r1 = Math.round(r1 * Array + r2 * (1 - Array) + 1);
            g1 = Math.round(g1 * Array + g2 * (1 - Array) + 1);
            b1 = Math.round(b1 * Array + b2 * (1 - Array) + 1);
            if (a1 > 255)
            {
                a1 = 255;
            }
            if (r1 > 255)
            {
                r1 = 255;
            }
            if (g1 > 255)
            {
                g1 = 255;
            }
            if (b1 > 255)
            {
                b1 = 255;
            }
            return (a1 << 24) + (r1 << 16) + (g1 << 8) + b1;
        }// end function

        public static function Antialias(param1:BitmapData, param2:Number, param3:Number)
        {
            fx = int(param2);
            fy = int(param3);
            cx = fx + 1;
            cy = fy + 1;
            lx = 1 - (param2 - fx);
            ly = 1 - (param3 - fy);
            p1 = CombinePixels(param1.getPixel32(fx, fy), param1.getPixel32(cx, fy), lx);
            p2 = CombinePixels(param1.getPixel32(fx, cy), param1.getPixel32(cx, cy), lx);
            return CombinePixels(p1, p2, ly);
        }// end function

        public static function Gaussian(Rectangle:Number) : Number
        {
            if (Rectangle < -0.5)
            {
                Rectangle = -1 - Rectangle;
                return 2 * Rectangle * Rectangle;
            }
            if (Rectangle < 0.5)
            {
                return 1 - 2 * Rectangle * Rectangle;
            }
            Rectangle = 1 - Rectangle;
            return 2 * Rectangle * Rectangle;
        }// end function

        public static function Square(Rectangle:Number) : Number
        {
            return Rectangle * Rectangle;
        }// end function

        public static function DegreesToRadian(Rectangle:Number) : Number
        {
            return Rectangle * (2 * Math.PI) / 360;
        }// end function

        public static function RadianToDegrees(Rectangle:Number) : Number
        {
            return Rectangle * 360 / (2 * Math.PI);
        }// end function

        public static function LookupTable(core.image:Misc/InflateBitmap:Number, core.image:Misc/InflateBitmap:Number) : Vector.<int>
        {
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_5:Number = NaN;
            var _loc_6:Number = NaN;
            var _loc_10:* = undefined;
            var _loc_11:* = undefined;
            var _loc_12:* = undefined;
            var _loc_7:* = new Vector.<int>;
            var _loc_8:Number = 0;
            var _loc_9:* = new Array(OVERSAMPLING);
            _loc_3 = OVERSAMPLING * Math.ceil(1 + Math.sqrt(2 * Square(Math.ceil((core.image:Misc/InflateBitmap + 1)))));
            if (1 - core.image:Misc/InflateBitmap < 4e-007)
            {
                _loc_6 = 1000000;
            }
            else
            {
                _loc_6 = 0.4 / (1 - core.image:Misc/InflateBitmap);
            }
            _loc_4 = 0;
            while (_loc_4 < OVERSAMPLING)
            {
                
                _loc_5 = Math.abs((_loc_4 + 0.5) / OVERSAMPLING - 0.5);
                if (_loc_5 > core.image:Misc/InflateBitmap)
                {
                    _loc_9[_loc_4] = 0;
                }
                else
                {
                    _loc_9[_loc_4] = Gaussian(Math.pow(_loc_5 / core.image:Misc/InflateBitmap, _loc_6));
                }
                _loc_8 = _loc_8 + _loc_9[_loc_4];
                _loc_4++;
            }
            _loc_4 = 0;
            while (_loc_5 < core.image:Misc/InflateBitmap || _loc_8 > 1e-005)
            {
                
                _loc_8 = _loc_8 - _loc_9[_loc_4 % OVERSAMPLING];
                if (_loc_5 > core.image:Misc/InflateBitmap)
                {
                    _loc_9[_loc_4 % OVERSAMPLING] = 0;
                }
                else
                {
                    _loc_9[_loc_4 % OVERSAMPLING] = Gaussian(Math.pow(_loc_5 / core.image:Misc/InflateBitmap, _loc_6));
                }
                _loc_8 = _loc_8 + _loc_9[_loc_4 % OVERSAMPLING];
                _loc_10 = _loc_4;
                _loc_4 = _loc_4 + 1;
                _loc_11 = _loc_10;
                _loc_7[_loc_11] = Math.floor(_loc_8 * (255 / OVERSAMPLING));
                _loc_5 = _loc_5 + 1 / OVERSAMPLING;
            }
            while (_loc_4 < _loc_3)
            {
                
                _loc_12 = _loc_4;
                _loc_4 = _loc_4 + 1;
                _loc_11 = _loc_12;
                _loc_7[_loc_11] = 0;
            }
            return _loc_7;
        }// end function

        public static function GenerateBrush(round:ShapeDetails) : Brush
        {
            var _loc_2:Vector.<int> = null;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            var _loc_7:Number = NaN;
            var _loc_8:Number = NaN;
            var _loc_9:Number = NaN;
            var _loc_10:Number = NaN;
            var _loc_11:Number = NaN;
            var _loc_12:Number = NaN;
            var _loc_13:Number = NaN;
            var _loc_14:Number = NaN;
            var _loc_15:Number = NaN;
            var _loc_16:Number = NaN;
            var _loc_17:Number = NaN;
            var _loc_19:int = 0;
            var _loc_20:int = 0;
            var _loc_18:* = new Array();
            var _loc_21:* = new Point(0, 0);
            var _loc_22:* = new Point(0, 0);
            _loc_8 = Math.sin(DegreesToRadian(round.angle));
            _loc_7 = Math.cos(DegreesToRadian(round.angle));
            _loc_11 = round.radius / round.aspect;
            _loc_21.x = _loc_7 * round.radius;
            _loc_21.y = -1 * _loc_8 * round.radius;
            _loc_22.x = _loc_8 * _loc_11;
            _loc_22.y = _loc_7 * _loc_11;
            switch(round.type)
            {
                case BrushType.Circle:
                {
                    _loc_19 = Math.ceil(Math.sqrt(_loc_21.x * _loc_21.x + _loc_22.x * _loc_22.x));
                    _loc_20 = Math.ceil(Math.sqrt(_loc_21.y * _loc_21.y + _loc_22.y * _loc_22.y));
                    break;
                }
                case BrushType.Square:
                {
                    _loc_19 = Math.ceil(Math.abs(_loc_21.x) + Math.abs(_loc_22.x));
                    _loc_20 = Math.ceil(Math.abs(_loc_21.y) + Math.abs(_loc_22.y));
                    break;
                }
                case BrushType.Diamond:
                {
                    _loc_19 = Math.ceil(Math.max(Math.abs(_loc_21.x), Math.abs(_loc_22.x)));
                    _loc_20 = Math.ceil(Math.max(Math.abs(_loc_21.y), Math.abs(_loc_22.y)));
                    break;
                }
                default:
                {
                    break;
                }
            }
            if (round.spikes > 2)
            {
                _loc_20 = Math.ceil(Math.sqrt(round.radius * round.radius + _loc_11 * _loc_11));
                _loc_19 = _loc_20;
                _loc_22.x = _loc_8 * round.radius;
                _loc_22.y = _loc_7 * round.radius;
            }
            var _loc_23:* = new Brush(round.type, new Vector.<int>((_loc_19 * 2 + 1) * (_loc_20 * 2 + 1), true), _loc_19 * 2 + 1, _loc_20 * 2 + 1, round.step, round);
            _loc_2 = LookupTable(round.radius, round.hardness);
            _loc_9 = Math.cos(-2 * Math.PI / round.spikes);
            _loc_10 = Math.sin(-2 * Math.PI / round.spikes);
            _loc_5 = -_loc_20;
            while (_loc_5 <= _loc_20)
            {
                
                _loc_4 = -_loc_19;
                while (_loc_4 <= _loc_19)
                {
                    
                    _loc_12 = 0;
                    _loc_13 = _loc_7 * _loc_4 - _loc_8 * _loc_5;
                    _loc_14 = Math.abs(_loc_8 * _loc_4 + _loc_7 * _loc_5);
                    if (round.spikes > 2)
                    {
                        _loc_17 = Math.atan2(_loc_14, _loc_13);
                        while (_loc_17 > Math.PI / round.spikes)
                        {
                            
                            _loc_16 = _loc_13;
                            _loc_15 = _loc_14;
                            _loc_13 = _loc_9 * _loc_16 - _loc_10 * _loc_15;
                            _loc_14 = _loc_10 * _loc_16 + _loc_9 * _loc_15;
                            _loc_17 = _loc_17 - 2 * Math.PI / round.spikes;
                        }
                    }
                    _loc_14 = _loc_14 * round.aspect;
                    switch(round.type)
                    {
                        case BrushType.Circle:
                        {
                            _loc_12 = Math.sqrt(Square(_loc_13) + Square(_loc_14));
                            break;
                        }
                        case BrushType.Square:
                        {
                            _loc_12 = Math.max(Math.abs(_loc_13), Math.abs(_loc_14));
                            break;
                        }
                        case BrushType.Diamond:
                        {
                            _loc_12 = Math.abs(_loc_13) + Math.abs(_loc_14);
                            break;
                        }
                        default:
                        {
                            break;
                        }
                    }
                    if (_loc_12 < (round.radius + 1))
                    {
                        _loc_3 = _loc_2[Math.floor(_loc_12 * OVERSAMPLING)];
                    }
                    else
                    {
                        _loc_3 = 0;
                    }
                    _loc_6 = (_loc_5 + _loc_20) * _loc_23.width + (_loc_4 + _loc_19);
                    if (_loc_3 < 0)
                    {
                        _loc_3 = 0;
                    }
                    _loc_23.data[_loc_6] = _loc_3;
                    _loc_4++;
                }
                _loc_5++;
            }
            return _loc_23;
        }// end function

        public static function ResizeBrush(round:Brush, round:int, round:int, round:int) : Brush
        {
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            var _loc_13:* = undefined;
            var _loc_8:* = new Reform();
            var _loc_9:* = new BitmapData(round.width, round.height, false, 0);
            var _loc_10:* = -1;
            _loc_6 = 0;
            while (_loc_6 < round.height)
            {
                
                _loc_5 = 0;
                while (_loc_5 < round.width)
                {
                    
                    _loc_10 = _loc_10 + 1;
                    _loc_7 = round.data[_loc_10];
                    _loc_9.setPixel(_loc_5, _loc_6, _loc_7 << 16 | _loc_7 << 8 | _loc_7);
                    _loc_5++;
                }
                _loc_6++;
            }
            _loc_9 = _loc_8.Resize(_loc_9, round / round.size, round / round.size);
            var _loc_11:* = new Vector.<int>(_loc_9.width * _loc_9.height, true);
            _loc_10 = -1;
            _loc_6 = 0;
            while (_loc_6 < _loc_9.height)
            {
                
                _loc_5 = 0;
                while (_loc_5 < _loc_9.width)
                {
                    
                    _loc_10 = _loc_10 + 1;
                    _loc_13 = _loc_10;
                    _loc_11[_loc_13] = _loc_9.getPixel(_loc_5, _loc_6) >>> 16 & 255;
                    _loc_5++;
                }
                _loc_6++;
            }
            var _loc_12:* = new Brush(BrushType.Custom, _loc_11, _loc_9.width, _loc_9.height, round, null);
            _loc_12.parent = round.parent ? (round.parent) : (round);
            return _loc_12;
        }// end function

        public static function IsTransparent(BitmapData:BitmapData) : Boolean
        {
            var _loc_2:int = 0;
            var _loc_3:Boolean = false;
            var _loc_4:int = 0;
            if (BitmapData == null)
            {
                return true;
            }
            while (_loc_4 < BitmapData.width)
            {
                
                _loc_2 = 0;
                while (_loc_2 < BitmapData.height)
                {
                    
                    if ((BitmapData.getPixel32(_loc_4, _loc_2) >>> 24 & 255) < 255)
                    {
                        _loc_3 = true;
                        break;
                    }
                    _loc_2++;
                }
                _loc_4++;
            }
            return _loc_3;
        }// end function

        public static function DrawCross(g1:BitmapData, g1:Point) : void
        {
            g1.setPixel32(g1.x - 2, g1.y, 4289374890);
            g1.setPixel32((g1.x - 1), g1.y, 4289374890);
            g1.setPixel32((g1.x + 1), g1.y, 4289374890);
            g1.setPixel32(g1.x + 2, g1.y, 4289374890);
            g1.setPixel32(g1.x, g1.y, 4289374890);
            g1.setPixel32(g1.x, g1.y - 2, 4289374890);
            g1.setPixel32(g1.x, (g1.y - 1), 4289374890);
            g1.setPixel32(g1.x, (g1.y + 1), 4289374890);
            g1.setPixel32(g1.x, g1.y + 2, 4289374890);
            return;
        }// end function

        public static function DrawPolygon(g1:BitmapData, g1:Vector.<Point>) : void
        {
            var _loc_3:Point = null;
            var _loc_4:Point = null;
            var _loc_5:int = 0;
            if (g1.length > 1)
            {
                _loc_3 = g1[0];
                _loc_5 = 1;
                while (_loc_5 < g1.length)
                {
                    
                    _loc_4 = g1[_loc_5];
                    DrawLine(g1, _loc_3, _loc_4);
                    _loc_3 = _loc_4;
                    _loc_5++;
                }
            }
            return;
        }// end function

        public static function DrawLine(g1:BitmapData, g1:Point, g1:Point) : void
        {
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            var _loc_8:int = 0;
            var _loc_9:int = 0;
            var _loc_10:int = 0;
            var _loc_11:int = 0;
            g1.lock();
            _loc_8 = g1.x;
            _loc_9 = g1.y;
            _loc_10 = g1.x - g1.x;
            _loc_11 = g1.y - g1.y;
            _loc_5 = _loc_10 > 0 ? (1) : (-1);
            _loc_6 = _loc_11 > 0 ? (1) : (-1);
            _loc_10 = _loc_10 < 0 ? (-_loc_10) : (_loc_10);
            _loc_11 = _loc_11 < 0 ? (-_loc_11) : (_loc_11);
            if (_loc_10 > _loc_11)
            {
                _loc_7 = _loc_10 * 0.5;
                _loc_4 = 1;
                while (_loc_4 <= _loc_10)
                {
                    
                    _loc_8 = _loc_8 + _loc_5;
                    _loc_7 = _loc_7 + _loc_11;
                    if (_loc_7 >= _loc_10)
                    {
                        _loc_7 = _loc_7 - _loc_10;
                        _loc_9 = _loc_9 + _loc_6;
                    }
                    g1.setPixel32(_loc_8, _loc_9, 4289374890);
                    _loc_4++;
                }
            }
            else
            {
                _loc_7 = _loc_11 * 0.5;
                _loc_4 = 1;
                while (_loc_4 <= _loc_11)
                {
                    
                    _loc_9 = _loc_9 + _loc_6;
                    _loc_7 = _loc_7 + _loc_10;
                    if (_loc_7 >= _loc_11)
                    {
                        _loc_7 = _loc_7 - _loc_11;
                        _loc_8 = _loc_8 + _loc_5;
                    }
                    g1.setPixel32(_loc_8, _loc_9, 4289374890);
                    _loc_4++;
                }
            }
            g1.unlock();
            return;
        }// end function

    }
}
